package net.zoneland.knowledge.service.impl;

import net.zoneland.knowledge.dao.SharedEditingFolderMapper;
import net.zoneland.knowledge.model.*;
import net.zoneland.knowledge.service.*;
import net.zoneland.oadocs.sdk.enums.FileTypeEnums;
import net.zoneland.oadocs.sdk.pojo.DocResponseInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.UUID;

/**
 * 共享编辑目录服务.
 *
 * @author cxz
 * @Date 2023/4/16 13:23
 */
@Service
public class SharedEditingFolderServiceImpl implements SharedEditingFolderService {
    /**
     * 日志.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(SharedEditingFolderServiceImpl.class);

    @Autowired
    private SharedEditingFolderMapper folderMapper;

    /**
     * 保存或者更新文件/目录信息.
     *
     * @param info 文件夹信息.
     */
    @Override
    public void saveOrUpdateFolder(final DocResponseInfo info) {
        final boolean fileNameExist = folderMapper.isFileNameExist(true, info.getFileName(), FileTypeEnums.FOLDER.getValue());
        final SharedEditingFolderInfo folderInfoNew = changeObject(info);
        //如果文件信息存在的话就更新
        if (fileNameExist) {
            folderMapper.updateFolder(folderInfoNew);
        } else {
            //否则就是新增
            folderInfoNew.setId(UUID.randomUUID().toString());
            folderMapper.insertFolder(folderInfoNew);
        }

    }

    /**
     * docResponseInfo转成 SharedEditingFolderInfo
     * @param docInfo
     * @return 共享编辑文件/文件夹信息.
     */
    private SharedEditingFolderInfo changeObject(final DocResponseInfo docInfo){
        final SharedEditingFolderInfo folderInfo = new SharedEditingFolderInfo();
        if (Objects.isNull(docInfo)){
            return folderInfo;
        }
        if(StringUtils.isNotBlank(docInfo.getParentId())) {
            folderInfo.setParentId(docInfo.getParentId());
        }
        if(StringUtils.isNotBlank(docInfo.getRootId())) {
            folderInfo.setRootId(docInfo.getRootId());
        }
        if(StringUtils.isNotBlank(docInfo.getExtend())) {
            folderInfo.setExtend(docInfo.getExtend());
        }
        if(StringUtils.isNotBlank(docInfo.getFileId())) {
            folderInfo.setFileId(docInfo.getFileId());
        }
        if(StringUtils.isNotBlank(docInfo.getFileName())) {
            folderInfo.setFileName(docInfo.getFileName());
        }
        if(StringUtils.isNotBlank(docInfo.getFileType())) {
            folderInfo.setFileType(docInfo.getFileType());
        }
        if(StringUtils.isNotBlank(docInfo.getPathId())) {
            folderInfo.setPathId(docInfo.getPathId());
        }
        if(StringUtils.isNotBlank(docInfo.getPathName())) {
            folderInfo.setPathName(docInfo.getPathName());
        }

        return folderInfo;
    }


    /**
     * 根据文件名查询文件夹信息.
     *
     * @param fileName 文档名称.
     * @return 返回共享编辑文件/文件夹信息.
     */
    @Override
    public SharedEditingFolderInfo queryFolder(final String fileName) {
        //根据状态，文件名，文件类型（这里是目录/文件夹）
        return folderMapper.findSharedEditingFolderInfo(true, fileName, FileTypeEnums.FOLDER.getValue());
    }
}
